/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.SeckillConfigDao;
import com.boxin.ims.modules.wechat.entity.SeckillConfig;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 秒杀配置Service
 * @author Jakemanse
 * @version 2013-09-28
 */
@Component
@Transactional(readOnly = true)
public class SeckillConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SeckillConfigService.class);
	
	@Autowired
	private SeckillConfigDao seckillConfigDao;
	
	public SeckillConfig get(Long id) {
		return seckillConfigDao.findOne(id);
	}
	
	public SeckillConfig getByUserId(Long userId){
		DetachedCriteria dc = seckillConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<SeckillConfig> result  = seckillConfigDao.find(dc);
		return (result==null || result.isEmpty()) ? null : result.get(0);
	}
	
	public Page<SeckillConfig> find(Page<SeckillConfig> page, SeckillConfig seckillConfig) {
		DetachedCriteria dc = seckillConfigDao.createDetachedCriteria();
		if (seckillConfig.getUser()!=null && seckillConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", seckillConfig.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(seckillConfig.getName())){
			dc.add(Restrictions.like("name", "%"+seckillConfig.getName()+"%"));
		}
		
		
		if (StringUtils.isNotEmpty(seckillConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+seckillConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", SeckillConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return seckillConfigDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SeckillConfig seckillConfig) {
		seckillConfigDao.save(seckillConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		seckillConfigDao.deleteById(id);
	}
	
}
